package com.yiwei.zhh.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.activity.MyYouHuiAllActivity;
import com.yiwei.zhh.adapter.YouhuiV2Adapter;
import com.yiwei.zhh.api.callback.YouhuiSelectContract;
import com.yiwei.zhh.api.requestInfo.YouhuiSelectRequest;
import com.yiwei.zhh.model.BonusModel;
import com.yiwei.zhh.util.OnLoadeMoreListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/9/20.
 */

public class YouhuiTypeFragment extends BaseFragment implements YouhuiSelectContract.View {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.nothing)
    TextView nothing;
    Unbinder unbinder;
    @BindView(R.id.img_empty)
    ImageView imgEmpty;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private YouhuiV2Adapter adapter;
    private int type;
    private YouhuiSelectRequest api;
    private boolean isFirst = true;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            api.getYouhui(page, 10, type + 1);
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_youhui;
    }

    @Override
    protected void initView() {
        api = YouhuiSelectRequest.create();
        api.attachView(this);
        type = getArguments().getInt("type");
        adapter = new YouhuiV2Adapter(getContext(), type + 1);
        list.setAdapter(adapter);
        list.addOnScrollListener(onLoadeMoreListener);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                api.getYouhui(1, 10, type + 1);
            }
        });
        api.getYouhui(1, 10, type + 1);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        if (refresh != null) {
            refresh.setRefreshing(false);
        }
    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void getYouhuiSuccess(int pageIndex, BonusModel model) {
        if (model.pager.recordCount != 0 && isFirst) {
            isFirst = false;
            ((MyYouHuiAllActivity) getActivity()).setTabText(type, model.pager.recordCount);
        }
        adapter.add(model.list, pageIndex == 1, model.borrow_amount);
        if (adapter.getmSource().size() == 0 && model.list.size() <= 0) {
            layoutEmpty.setVisibility(View.VISIBLE);
            if(type==1){
                imgEmpty.setImageResource(R.mipmap.img_youhui_status_used);
                nothing.setText("哇，竟然没有优惠券使用记录");
            }else {
                imgEmpty.setImageResource(R.mipmap.img_youhui_status_use);
            }
        } else {
            layoutEmpty.setVisibility(View.GONE);
        }
    }
}
